package com.cws.sys;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.servlet.ServletContext;

/**
 * @title [内存静态信息]
 * @description [一句话描述]
 * @copyright Copyright 2013 SHIPING INFO Corporation. All rights reserved.
 * @company SHIPING INFO.
 * @author Caspar Du
 * @version v 1.0
 * @create 2013-6-9 下午10:53:43
 */
public class Mem {
	
	/**
	 * 系统全局配置
	 */
	private static Properties config;
	

	/**
	 * token信息
	 */
	private static ConcurrentMap<String, Long> tokenInfo = new ConcurrentHashMap<String, Long>();

	/**
	 * 上传的所有文件链表
	 */
	private static List<File> upLoadFileList = new ArrayList<File>();

	/**
	 * 工程上下文路径
	 */
	private static String ServletContextPath;

	/**
	 * 有效的计划任务信息
	 */
	private static Set<String> validTaskInfo = new HashSet<String>();

	/**
	 * 
	 */
	private static ServletContext servletContext;


	/**
	 * 
	 * @param jobName
	 */
	public synchronized static void addValidTask(String jobName) {
		validTaskInfo.add(jobName);
	}

	/**
	 * 
	 * @param jobName
	 */
	public synchronized static void removeValidTask(String jobName) {
		validTaskInfo.remove(jobName);
	}

	public static ConcurrentMap<String, Long> getTokenInfo() {
		return tokenInfo;
	}

	public static void setTokenInfo(ConcurrentMap<String, Long> tokenInfo) {
		Mem.tokenInfo = tokenInfo;
	}


	public static String getServletContextPath() {
		return ServletContextPath;
	}

	public static void setServletContextPath(String servletContextPath) {
		ServletContextPath = servletContextPath;
	}

	public static ServletContext getServletContext() {
		return servletContext;
	}

	public static void setServletContext(ServletContext servletContext) {
		Mem.servletContext = servletContext;
	}

	public static Set<String> getValidTaskInfo() {
		return validTaskInfo;
	}

	public static void setValidTaskInfo(Set<String> validTaskInfo) {
		Mem.validTaskInfo = validTaskInfo;
	}

	/**
	 * 添加token信息到内存
	 * 
	 * @param tokenId
	 */
	public static synchronized void addToken(String tokenId) {
		tokenInfo.put(tokenId, System.currentTimeMillis());
	};

	/**
	 * 删除token信息
	 * 
	 * @param tokenId
	 */
	public static synchronized void removeToken(String tokenId) {
		tokenInfo.remove(tokenId);
	};

	/**
	 * 更新token最后访问时间
	 * 
	 * @param tokenId
	 */
	public static synchronized void resetTokenTime(String tokenId, long time) {
		removeToken(tokenId);
		tokenInfo.put(tokenId, time);
	}

	public synchronized static List<File> getUpLoadFileList() {
		return upLoadFileList;
	}

	public synchronized static void setUpLoadFileList(List<File> upLoadFileList) {
		Mem.upLoadFileList = upLoadFileList;
	}


	public static Properties getConfig() {
		return config;
	}

	public static void setConfig(Properties config) {
		Mem.config = config;
	}
}
